package com.clkj.nangang.mvp.index;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.clkj.nangang.R;
import com.clkj.nangang.common.Constant;
import com.clkj.nangang.mvp.base.BaseFragment;
import com.clkj.nangang.mvp.index.aboutme.AboutMeActivity;
import com.clkj.nangang.mvp.index.appeal.AddAppealActivity;
import com.clkj.nangang.mvp.index.communityservice.CommunityServiceActivity;
import com.clkj.nangang.mvp.index.partymember.PartyMemberActivity;
import com.clkj.nangang.mvp.index.upperpolicy.UpperPolicyActivity;
import com.clkj.nangang.util.CommonUtil;
import com.clkj.nangang.util.LogUtil;
import com.clkj.nangang.util.viewutil.PicassoBannerLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * 政策指南中 办事要件，政策解读，服务指南 下的父级资讯类型
 * Created by Administrator on 2017/7/4.
 */

public class FragmentIndex extends BaseFragment implements View.OnClickListener {

    //默认的是isFirstVisibles true ,刚进来的时候分两种情况
    //1.如果切过来的时候 setVisibleHint在OnCreate之前调用，那isFirstVisible为true，这个时候不进行任何操作；
    //等方法执行到onActivityCreated的时候，在执行一次setUserVisibleHint 这个时候把默认的是isFirstVisibles 设为false
    //然后这个时候调用setUserVISIBLEhINT为true  去获取数据
    //2.如果是后面再次切过来，那isFirstVisible已经为false，那就可以直接通过setUserVisibleHint获取数据了

    //代表这个界面是不是第一次被使用
    boolean isFirstUsed = true;

    private TextView tvSolve;
    private Banner mainBanner;
    private TextView tvWywtxbm;
    private TextView tvWykkzbs;
    private TextView tvXyxdsh;
    private TextView tvDwjwsys;
    private TextView tvYwygnxs;
    private TextView tvWsdysgj;
    private TextView tvKksjzms;
    private TextView tvTitle;

    String mNewsKindBelong;

    private static final String NEWS_BELONG = "news_belong";


    public static FragmentIndex newInstance(String newsBelong) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(NEWS_BELONG, newsBelong);
        FragmentIndex fragment = new FragmentIndex();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mNewsKindBelong = bundle.getString(NEWS_BELONG);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_index, container, false);
        assignView(view);
        initData();
        initView();

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //fragment 可见的情况下 进行加载数据。不可见则不加载
        if (getUserVisibleHint() == true) {
            if (isFirstUsed == true) {
                isFirstUsed = false;
                setUserVisibleHint(true);
            }
            //不可见，说明fragment已经初始话完成,就直接把isFirstVisible改为false 再次切过来的时候就直接可以加载数据了
        } else {
            isFirstUsed = false;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        // LogUtil.e("CartVisible");
        if (getUserVisibleHint()) {
            LogUtil.e("MyShuoShuoVisible");
            //如果不是firstvisible才去进行下面的操作  因为第一次很可能出现各种问题
            if (isFirstUsed == false) {

            }
        } else {
            LogUtil.e("faXianFragmentVisible");
        }
    }


    /***
     * 判断用户是普通用户还是工作人员 工作人员才有诉求处理按钮
     */
    private void checkRoleOfUser() {
        // TODO Auto-generated method stub
        if (TextUtils.equals("用户", (CharSequence) CommonUtil.getShareValue(Constant.NANGAN_ROLE, ""))) {
            tvSolve.setVisibility(View.GONE);
        } else if (TextUtils.equals("管理人员", (CharSequence) CommonUtil.getShareValue(Constant.NANGAN_ROLE, ""))) {
            tvSolve.setVisibility(View.VISIBLE);
        } else {
            tvSolve.setVisibility(View.GONE);
        }
    }

    @Override
    public void initData() {

        //获取用户角色
        checkRoleOfUser();

        //赋banner图片
        List<Integer> newsPicUrls = new ArrayList<>();
        newsPicUrls.add(R.drawable.icon_shequ);
        newsPicUrls.add(R.drawable.icon_banner);
        mainBanner.setImages(newsPicUrls)
                .setImageLoader(new PicassoBannerLoader())
                .setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
                .setDelayTime(5000)
                .start();
    }

    @Override
    public void assignView(View view) {

        tvTitle = (TextView) view.findViewById(R.id.tv_title);
        tvSolve = (TextView) view.findViewById(R.id.tv_right);
        mainBanner = (Banner) view.findViewById(R.id.main_banner);
        tvWywtxbm = (TextView) view.findViewById(R.id.tv_wywtxbm);
        tvWykkzbs = (TextView) view.findViewById(R.id.tv_wykkzbs);
        tvXyxdsh = (TextView) view.findViewById(R.id.tv_xyxdsh);
        tvDwjwsys = (TextView) view.findViewById(R.id.tv_dwjwsys);
        tvYwygnxs = (TextView) view.findViewById(R.id.tv_ywygnxs);
        tvWsdysgj = (TextView) view.findViewById(R.id.tv_wsdysgj);
        tvKksjzms = (TextView) view.findViewById(R.id.tv_kksjzms);

        tvTitle.setText("南港快捷通");
        tvSolve.setText("处理诉求");

    }

    @Override
    public void initView() {

        tvWywtxbm.setOnClickListener(this);
        tvSolve.setOnClickListener(this);
        tvWykkzbs.setOnClickListener(this);
        tvXyxdsh.setOnClickListener(this);
        tvDwjwsys.setOnClickListener(this);
        tvYwygnxs.setOnClickListener(this);
        tvWsdysgj.setOnClickListener(this);
        tvKksjzms.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            //管理员查看诉求
            case R.id.tv_right:

                break;
            //我有问题需帮忙
            case R.id.tv_wywtxbm:
                intent = new Intent(getActivity(), AddAppealActivity.class);
                startActivity(intent);
                break;
            //我要看看周边事
            case R.id.tv_wykkzbs:

                break;
            //学一学得
            case R.id.tv_xyxdsh:

                break;
            //党务局务晒一晒
            case R.id.tv_dwjwsys:
                intent = new Intent(getActivity(), CommunityServiceActivity.class);
                startActivity(intent);
                break;
            //与我有关那些事
            case R.id.tv_ywygnxs:
                intent = new Intent(getActivity(), AboutMeActivity.class);
                startActivity(intent);
                break;
            //我是党员守规矩
            case R.id.tv_wsdysgj:
                intent = new Intent(getActivity(), PartyMemberActivity.class);
                startActivity(intent);
                break;
            //看看上级说
            case R.id.tv_kksjzms:
                intent = new Intent(getActivity(), UpperPolicyActivity.class);
                startActivity(intent);
                break;
        }
    }
}

